﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Treely.Data.Model;
using Treely.Data.Model.Abstract;
using Treely.DataAccess;

namespace Treely.Data.Repositories
{
    public class AssessmentRepository : Repository, IRepository<Assessment>
    {
        public IEnumerable<Assessment> Get()
        {
            return Context.Assessments.AsEnumerable();
        }

        public Assessment Get(int id)
        {
            return Context.Assessments.FirstOrDefault(x => x.AssessmentId == id);
        }

        public void Store(Assessment a)
        {
            if (a.AssessmentId > 0)
            {
                var target = Context.Assessments.FirstOrDefault(x => x.AssessmentId == a.AssessmentId);
                if (target == null)
                    Add(a);
                Mapper.Map(a, target);
            }
            else
            {
                Add(a);
            }
        }

        public void Delete(int targetId, string userId)
        {
            var target = Context.Assessments.FirstOrDefault(x => x.AssessmentId == targetId);
            if (target == null)
                throw new ArgumentException("Not a valid id.");
            target.Delete(userId);
        }

        protected void Add(Assessment a)
        {
            Context.Assessments.Add(a);
        }

        public AssessmentRepository(ApplicationDbContext db)
            : base(db)
        {
        }
    }
}